import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';

import { BASE_CONFIG } from '@base/services/config.service';

@NgModule()
export class BaseModule
{
    constructor(@Optional() @SkipSelf() parentModule: BaseModule)
    {
        if ( parentModule )
        {
            throw new Error('BaseModule is already loaded. Import it in the AppModule only!');
        }
    }

    static forRoot(config): ModuleWithProviders<any>
    {
        return {
            ngModule : BaseModule,
            providers: [
                {
                    provide : BASE_CONFIG,
                    useValue: config
                }
            ]
        };
    }
}
